package edu.spec.student.dao.impl;

import edu.spec.student.dao.iface.IStudentEventDao;
import edu.spec.student.model.Student;
import edu.spec.student.model.event.Event;
import edu.spec.student.model.event.StudentEvent;
import edu.spec.basic.model.enumeration.AttendStatus;
import org.hibernate.Query;
import org.hibernate.Session;
import org.madgecko.basic.dao.BaseDao;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * Created by MadFrog on 2017/6/12.
 */
@Repository
public class StudentEventDao extends BaseDao<StudentEvent> implements IStudentEventDao {

    @Override
    public List<Event> listUnAttendEvent(Integer studentId) {
        String hql = " select se.event from StudentEvent se where se.student.id=" + studentId + " and se.event.eventDate>:currentDate";
        Session s = this.getSession();
        Query query = s.createQuery(hql.toString());
        query.setTimestamp("currentDate", new Date());//注意这里，由于要保留时分秒，用setTimestamp，不要用setDate
        List<Event> attendList = query.list();
        return attendList;
    }

    @Override
    public List<Event> listAttendedEvent(Integer studentId) {
        String hql = " select se.event from StudentEvent se where se.student.id=" + studentId + " and se.event.eventDate<=:currentDate";
        Session s = this.getSession();
        Query query = s.createQuery(hql.toString());
        query.setTimestamp("currentDate", new Date());//注意这里，由于要保留时分秒，用setTimestamp，不要用setDate
        List<Event> attendList = query.list();
        return attendList;
    }

    @Override
    public void signIn(String openId, Integer eventId) {
        String hql = "update StudentEvent se set se.signIn=1 where se.student.openId='" + openId + "' and se.event.id=" + eventId;
        this.updateByHql(hql);
    }

    @Override
    public StudentEvent load(String openId, Integer eventId) {
        String hql = "select se from StudentEvent se where se.student.openId='" + openId + "' and se.event.id=" + eventId;
        return (StudentEvent)this.queryObject(hql);
    }

    @Override
    public void deleteByEvent(Integer eventId) {
        String hql = "delete StudentEvent se where se.event.id=" + eventId;
        this.updateByHql(hql);
    }

    @Override
    public List<Student> listStudentByCon(Integer eventId, Integer clazzId, AttendStatus attendStatus) {
        String hql = "select se.student from StudentEvent se where se.event.id=" + eventId + " and se.student.bjid=" + clazzId;

        // 未报名
        if (attendStatus==AttendStatus.UNENROLL){
            hql = "select se.student.id from StudentEvent se where se.event.id=" + eventId + " and se.student.bjid=" + clazzId;
            List<Integer> enrollIds = this.listIdByHql(hql);
            hql = "from Student s where s.bjid=" + clazzId + " and s.id not in(:?)";
            System.out.println(hql);
            return this.list(hql, enrollIds);
        }
        //未签到
        else if (attendStatus==AttendStatus.UNSIGNIN){
            hql += " and se.signIn<>1";
        }
        //正常参加
        if (attendStatus==AttendStatus.ATTEND){
            hql += " and se.signIn=1";
        }
        return this.list(hql);
    }

    @Override
    public List<Student> listStudentByCon(Integer eventId) {
        String hql = "";
        // 1.未报名
        hql = "from Student s where s.id not in(select se.student.id from StudentEvent se where se.event.id=" + eventId + ")";
        System.out.println(hql);
        List<Student> unAttendStudent = this.list(hql);
        for(Student s : unAttendStudent){
            s.setAttendStatus(AttendStatus.UNENROLL);
        }
        // 2.未签到
        hql = "select se.student from StudentEvent se where se.event.id=" + eventId + " and se.signIn=null";
        List<Student> unsignInStudent = this.list(hql);
        for(Student s : unsignInStudent){
            s.setAttendStatus(AttendStatus.UNSIGNIN);
        }
        // 3.正常参加
        hql = "select se.student from StudentEvent se where se.event.id=" + eventId + " and se.signIn=1";
        List<Student> attendStudent = this.list(hql);
        for(Student s : attendStudent){
            s.setAttendStatus(AttendStatus.ATTEND);
        }
        unAttendStudent.addAll(unsignInStudent);
        unAttendStudent.addAll(attendStudent);
        return unAttendStudent;
    }

    public void countAttend(Integer eventId){
        // 分专业统计参加人数
        String hql = " select new edu.spec.sms.model.event.SumDto(se.student.clazz.major.name, count(*)) from StudentEvent se where se.event.id=1 group by se.student.clazz.major";
        List major = this.list(hql);
        System.out.println(major.size());
    }

}
